<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
    
<body>

</body>

</html>
<script>
    (function () {
        function observe() {
            //保存多个消息类型和对应的回调函数
            this.obj = {}
            //订阅消息
            this.on = function (type, handle) {
                // this.obj[type] || []
                if (this.obj[type]) {
                    this.obj[type].push(handle)
                } else {
                    this.obj[type] = [handle]
                }
            }
            //发布消息
            this.emit = function (type, msg) {
                if (this.obj[type]) {
                    this.obj[type].forEach((item) => {
                        item(msg)
                    })
                }
            }
        }
        window.observe = observe
    })()
    var os = new observe()
    os.on('ysb',function(msg){
        console.log('ysb的'+msg);
    })
    os.emit('ysb', '不想上课')
</script>